﻿Public Class FOPenilaianSiswa

    'member variables
    Private _cGuruMengajar As CGuruMengajar
    Private _semesterAktif As tbl_semester
    Private _cSemester As CSemester


    'forms
    Private WithEvents _flSemester As FLSemester
    Private WithEvents _flGuru As FLGuru
    Private _parent As Form


#Region "SettinganAwal"
    '-----------------------------------------------------------------------------------------------
    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles LabelJudul.MouseDown, MyBase.MouseDown
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp, LabelJudul.MouseUp
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove, LabelJudul.MouseMove
        If IsFormBeingDragged Then
            Dim temp As Point = New Point()

            temp.X = Me.Location.X + (e.X - MouseDownX)
            temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = temp
            temp = Nothing
        End If
    End Sub

    Private Sub Panel1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim p As New Pen(Color.FromArgb(213, 251, 185), 2)
        'set text box border
        PaintTextBox(Me, e.Graphics, p)

        p.Dispose()
    End Sub

    Private Sub PaintTextBox(cont As Control, g As Graphics, p As Pen)
        For Each ctl As Control In cont.Controls
            If ctl.Visible Then
                p.Color = ctl.BackColor
                If (TypeOf ctl Is TextBox) Or (TypeOf ctl Is ComboBox) Or (TypeOf ctl Is PictureBox) Then
                    g.DrawRectangle(p, New Rectangle(ctl.Location + New Size(1, 1), ctl.Size - New Size(2, 2)))
                ElseIf TypeOf ctl Is Panel Then
                    PaintTextBox(ctl, g, p)
                ElseIf TypeOf ctl Is GroupBox Then
                    PaintTextBox(ctl, g, p)
                End If
            End If

        Next
    End Sub

    Private Sub SetRegionTextBox(cont As Control)
        For Each ctl As Control In cont.Controls
            If (TypeOf ctl Is TextBox) Or (TypeOf ctl Is ComboBox) Or (TypeOf ctl Is PictureBox) Then
                If ctl.Region Is Nothing Then
                    ctl.Region = New Region(New Rectangle(2, 2, ctl.Width - 4, ctl.Height - 4))
                Else
                    ctl.Region = Nothing
                End If
            ElseIf TypeOf ctl Is Panel Then
                SetRegionTextBox(ctl)
            ElseIf TypeOf ctl Is GroupBox Then
                SetRegionTextBox(ctl)
            End If
        Next
    End Sub


    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    '-----------------------------------------------------------------------------------------------
#End Region

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Public Sub New(parent As Form)
        Me.New()
        _parent = parent
    End Sub

    Private Sub tbNama_Enter(sender As Object, e As System.EventArgs) Handles tbSemester.Enter, tbSemester.Click
        'tampilkan form list semester
        _flSemester = New FLSemester
        _flSemester.ShowDialog()
    End Sub

    Private Sub tbNama_TextChanged(sender As System.Object, e As System.EventArgs)

    End Sub

    Private Sub btnKeluar_Click(sender As System.Object, e As System.EventArgs) Handles btnKeluar.Click
        Me.Close()
    End Sub

    Private Sub _flSemester_DataTerpilih() Handles _flSemester.DataTerpilih
        'set data semester terpilih
        tbSemester.Text = _flSemester.SelectedNamaSemester
        tbSemesterValue.Text = _flSemester.SelectedIDSemester
    End Sub

    Private Sub tbGuru_Enter(sender As Object, e As System.EventArgs) Handles tbGuru.Enter, tbGuru.Click
        _flGuru = New FLGuru
        _flGuru.ShowDialog()
    End Sub

    Private Sub _flGuru_DataTerpilih() Handles _flGuru.DataTerpilih
        'set data guru terpilih
        tbGuru.Text = _flGuru.SelectedNamaGuru
        tbGuruValue.Text = _flGuru.SelectedIDGuru

        'bersihkan combo kelas dan MP
        cbKelas.DataSource = Nothing
        cbMP.DataSource = Nothing

        'set kelas yang di ajar
        _cGuruMengajar = New CGuruMengajar
        With cbKelas
            .DataSource = _cGuruMengajar.getKelas(tbGuruValue.Text.Trim, tbSemesterValue.Text.Trim)
            .DisplayMember = "nama"
            .ValueMember = "id"
        End With
    End Sub

    Private Sub cbKelas_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbKelas.SelectedIndexChanged
        'set mata pelajaran yang di ajar guru
        Try
            If cbKelas.Items.Count > 0 Then
                With cbMP
                    .DataSource = _cGuruMengajar.getMP(tbGuruValue.Text.Trim, tbSemesterValue.Text.Trim, cbKelas.SelectedValue)
                    .ValueMember = "id"
                    .DisplayMember = "nama"
                End With
            End If
        Catch ex As Exception
            If ex.Message.Contains("DataRowView") Then
            Else
                MsgBox(ex.Message)
            End If
        End Try


    End Sub

    Private Sub tbJenis_Enter(sender As Object, e As System.EventArgs) Handles tbJenis.Enter, tbJenis.Click
        ContextMenuStripJenis.Show(tbJenis, 0, 0)
    End Sub

    Private Sub ContextMenuStripJenis_ItemClicked(sender As Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ContextMenuStripJenis.ItemClicked
        'set jenis
        tbJenis.Text = e.ClickedItem.Text
        tbJenisValue.Text = e.ClickedItem.Tag
    End Sub

    Private Sub tbBln_TextChanged(sender As System.Object, e As System.EventArgs) Handles tbBln.Enter, tbBln.Click
        ContextMenuStripBulan.Show(tbBln, 0, 0)
    End Sub

    Private Sub ContextMenuStripBulan_ItemClicked(sender As Object, e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ContextMenuStripBulan.ItemClicked
        'set bulan value
        tbBlnvalue.Text = e.ClickedItem.Tag
        tbBln.Text = e.ClickedItem.Text
    End Sub

    Private Sub btnSimpan_Click(sender As System.Object, e As System.EventArgs) Handles btnOk.Click
        'tampilkan form penilaian
        Dim _ftpenilaian As New FTPenilaian(tbGuruValue.Text.Trim, DateSerial(tbThn.Text.Trim, tbBlnvalue.Text.Trim, tbTgl.Text.Trim), tbJenisValue.Text.Trim, cbKelas.SelectedValue, cbMP.SelectedValue, tbSemesterValue.Text)
        With _ftpenilaian
            .MdiParent = _parent
            '.WindowState = FormWindowState.Maximized
            .Show()
        End With

        Me.Close()
    End Sub

    Private Sub FOPenilaianSiswa_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'set text box border
        SetRegionTextBox(Me)

        'set semester aktif
        _cSemester = New CSemester
        _semesterAktif = _cSemester.getAktifSemester
        tbSemester.Text = _semesterAktif.nama
        tbSemesterValue.Text = _semesterAktif.id
    End Sub

    Private Sub OKToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs)
        btnOk.PerformClick()
    End Sub

    Private Sub OkToolStripMenuItem_Click_1(sender As System.Object, e As System.EventArgs) Handles OkToolStripMenuItem.Click
        btnOk.PerformClick()
    End Sub

    Private Sub RubahToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles RubahToolStripMenuItem.Click
        btnRubah.PerformClick()
    End Sub

    Private Sub KeluarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles KeluarToolStripMenuItem.Click
        btnKeluar.PerformClick()
    End Sub

    Private Sub btnRubah_Click(sender As System.Object, e As System.EventArgs) Handles btnRubah.Click
        Dim _frubahnilai As New FORubahNilai(_parent)
        _frubahnilai.ShowDialog()
        Me.Close()
    End Sub

End Class